const { getArtMovementById } = require("../../../utils/request");
const config = require("../../../utils/config");

Page({
    data: {
      activeTab: 'description', // 默认显示描述信息
      info: {}, // 艺术主题的详细信息
      artworksData: {
        list: [], // 艺术作品数据
        pageNum: 1,
        pageSize: 10,
        isLoading: false,
        hasMore: true,
      },
      artistsData: {
        list: [], // 艺术家数据
        pageNum: 1,
        pageSize: 10,
        isLoading: false,
        hasMore: true,
      },
    },
  
    onLoad: function (options) {
      const id = options.id;
      this.loadArtTopic(id); // 加载艺术主题数据
    },
  
    // 加载艺术主题数据
    loadArtTopic: function (id) {
        getArtMovementById({ id }).then(res => {
        if (res.code === '00000') {
          const info = {
            ...res.data,
            imageUrl: config.ImageURL + res.data.imageUrl,
          };
          this.setData({
            info,
          });
        } else {
          wx.showToast({
            title: '加载失败，请重试',
            icon: 'none',
          });
        }
      }).catch(err => {
        wx.showToast({
          title: '网络错误，请重试',
          icon: 'none',
        });
      });
    },
  
    // 处理Tab切换事件
    onTabChange: function (e) {
      const tab = e.detail.tab;
      this.setData({ activeTab: tab });
  
      // 重置滚动位置
      // wx.pageScrollTo({ scrollTop: 0, duration: 300 });
  
      // 如果切换到艺术作品或艺术家Tab，且数据为空，则加载数据
      if (tab === 'artwork' && this.data.artworksData.list.length === 0) {
        this.loadArtworks();
      } else if (tab === 'artist' && this.data.artistsData.list.length === 0) {
        this.loadArtists();
      }
    },
  
    // 加载艺术作品数据
    loadArtworks: function () {
      const { artworksData } = this.data;
      if (artworksData.isLoading || !artworksData.hasMore) return;
  
      this.setData({
        'artworksData.isLoading': true,
      });
  
      const { pageNum, pageSize } = artworksData;
      const id = this.data.info.id;
  
      getArtMovementById({ id, pageNum, pageSize }).then(res => {
        if (res.code === '00000') {
          const newArtworks = (res.data.artworks || []).map(item => ({
            ...item,
            primaryImageSmallUrl: config.ImageURL + item.primaryImageSmallUrl,
          }));
  
          this.setData({
            'artworksData.list': [...artworksData.list, ...newArtworks],
            'artworksData.hasMore': newArtworks.length >= pageSize,
            'artworksData.pageNum': pageNum + 1,
          });
        } else {
          wx.showToast({
            title: '加载失败，请重试',
            icon: 'none',
          });
        }
      }).catch(err => {
        wx.showToast({
          title: '网络错误，请重试',
          icon: 'none',
        });
      }).finally(() => {
        this.setData({
          'artworksData.isLoading': false,
        });
      });
    },
  
    // 加载艺术家数据
    loadArtists: function () {
      const { artistsData } = this.data;
      if (artistsData.isLoading || !artistsData.hasMore) return;
  
      this.setData({
        'artistsData.isLoading': true,
      });
  
      const { pageNum, pageSize } = artistsData;
      const id = this.data.info.id;
  
      getArtMovementById({ id, pageNum, pageSize }).then(res => {
        if (res.code === '00000') {
          const newArtists = (res.data.artists || []).map(item => ({
            ...item,
            photoUrl: config.ImageURL + item.photoUrl,
          }));
  
          this.setData({
            'artistsData.list': [...artistsData.list, ...newArtists],
            'artistsData.hasMore': newArtists.length >= pageSize,
            'artistsData.pageNum': pageNum + 1,
          });
        } else {
          wx.showToast({
            title: '加载失败，请重试',
            icon: 'none',
          });
        }
      }).catch(err => {
        wx.showToast({
          title: '网络错误，请重试',
          icon: 'none',
        });
      }).finally(() => {
        this.setData({
          'artistsData.isLoading': false,
        });
      });
    },
  
    // 监听页面滚动到底部
    onReachBottom: function () {
      console.log("onReachBottom()方法")
      const { activeTab } = this.data;
      if (activeTab === 'artwork') {
        this.loadArtworks();
      } else if (activeTab === 'artist') {
        this.loadArtists();
      }
    },
  });